package org.tony.utils;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tony.db.DbUtil;

/**
 * 用户登录
 * @author tony
 *
 */
public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1208011911649035506L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username") ;
		String password = request.getParameter("password") ;
		String url = null ;
		if((username!=null && username.length()>0) && (password!=null && password.length()>0)) {
			request.getSession().setAttribute("user", username) ;
			 url = DbUtil.login(username, password);
			 if(url==null) {
				 request.setAttribute("error", "登陆失败，错误的用户名或密码") ;
				 url = "login.jsp" ;
			 }
		}else {
			request.setAttribute("error", "登陆失败，错误的用户名或密码") ;
			url = "login.jsp" ;
		}
		request.getRequestDispatcher("/"+url).forward(request,response) ;
	}

}
